#include "cla_out.h"
#include<QDebug>
#include<QFile>
#include<QFileDialog>
#include<QMessageBox>
#include<QTextStream>
#include<QDebug>
#include<QTextEdit>
#include<QLineEdit>
#include<QTextCodec>
cla_out::cla_out()
{

}
bool cla_out::inqOut(QString fileName,QStringList &textlist,int &r_count){
    bool sign=0;
    if(!fileName.isEmpty()){
        sign=1;
        QFile file;
        file.setFileName(fileName);
        if(!file.open(QIODevice::ReadOnly|QIODevice::Text)){
            QMessageBox msg;
            msg.setText("打开文件失败");
            msg.exec();
        }else {
           if(!file.size()){
               QMessageBox::warning(0,QString("警告"),QString("文件大小为空："),QMessageBox::Ok);

           }else{
               QTextStream in(&file);
               in.setCodec("UTF-8");
               while(!in.atEnd()){
                   QString line=in.readLine();
                   textlist.append(line);
                   r_count++;
               }
               file.close();                         //将文件内容保存在 QStringlist中
        }
     }
   }
    return sign;
}
void cla_out::entOut(QStringList list,QString filepath){
    
    QFile file(filepath);
    file.open(QIODevice::ReadOnly|QIODevice::Text);
    QTextStream in(&file);
    in.setCodec("UTF-8");
    QString str;
    str=in.readLine();
    file.close();
                                                     //先判断文件是不是空，决定了要不要写入表头
    if(!str.isEmpty()){
        list.pop_front();                                 //文件不为空，表头出
        QTextEdit textEdit;
        for(int i=0;i<list.size();i++){
            textEdit.append(list.at(i));
        }
        if(file.open(QIODevice::Append|QIODevice::Text)){
            QTextStream ts(&file);
            ts.setCodec("UTF-8");
            ts<<'\n';
            ts<<textEdit.document()->toPlainText();
            file.close();
            QMessageBox::information(0,QString("提示"),QString("保存成功"),QMessageBox::Yes);
        }

    }else{                                                //文件为空,保留表头
        QTextEdit textEdit;
        for(int i=0;i<list.size();i++){
            textEdit.append(list.at(i));
        }
        if(file.open(QIODevice::Append|QIODevice::Text)){
            QTextStream ts(&file);
            ts.setCodec("UTF-8");
            ts<<textEdit.document()->toPlainText();
            file.close();                                //将表格内容写入文件保存
            QMessageBox::information(0,QString("提示"),QString("保存成功"),QMessageBox::Yes);
        }
    }
}
